<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\Api\Repositories\WxCommentRepositores;
use App\Lib\JwtAuth;
use App\Models\Posts\WxComment;
use App\Models\WxCommentLike;
use App\Models\WxNotice as NoticeModel;
use App\Models\WxSearch;
use App\Models\WxUser;
use Illuminate\Http\Request;

class CommentController extends BaseController
{
    /**
     * @param $posts_id 文章id
     * @param $comment_id 评论id
     * @param $user_id 评论的用户id
     * @param $comment_content 评论内容
     * @param Request $request
     */
    public function add(Request $request)
    {
        $posts_id = $request->posts_id;
        $user_id = $request->uid;
        $reply_user_id = $request->input('reply_user_id', '');
        $comment_id = $request->input('comment_id', '');
        $comment_content = $request->comment_content;
        $comment_img_url = $request->comment_img_url;
        return $this->success(WxCommentRepositores::add($posts_id, $comment_id,$reply_user_id, $user_id,  $comment_content, $comment_img_url));
    }

    /**
     * 通过帖子获取评论列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCommentByPostsId(Request $request)
    {
        $posts_id = $request->posts_id;
        $token = $request->header('token');
        $uid = 0;
        if ($token) {
            if (JwtAuth::verifyToken($token)) {
                $uid = JwtAuth::getUserId($token, 'uid');
            }
        }
        return $this->success(WxCommentRepositores::getCommentByPostsId($posts_id, ['id', 'user_name', 'comment_content', 'user_id', 'posts_user_id', 'user_avatar', 'comment_img_url', 'created_at'], $uid));
    }

    /**
     * 点赞评论
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function commentAddLike(Request $request)
    {
        $uid = $request->uid;
        $comment_id = $request->comment_id;

        $comment_user_id = WxComment::where('id', $comment_id)->value('user_id');

        $likeIsExists = WxCommentLike::where('user_id',$uid)
            ->where('comment_id',$comment_id)
            ->where('comment_user_id',$comment_user_id)->exists();
        // 判断是否已经点过赞
        if($likeIsExists){
            WxCommentLike::where('user_id',$uid)
                ->where('comment_id',$comment_id)
                ->where('comment_user_id',$comment_user_id)->delete();

            return $this->success([],416004);
        }


        $CommentLikeModel = new WxCommentLike();
        $CommentLikeModel->user_id = $uid;
        $CommentLikeModel->comment_id = $comment_id;
        $CommentLikeModel->comment_user_id = $comment_user_id;
        $CommentLikeModel->save();


        $user = WxUser::where('id', $uid)->first('user_name');
        $comment = WxComment::where('id', $comment_id)->first();

        $noticeModel = new NoticeModel;
        $noticeModel->user_id = $comment_user_id;
        $noticeModel->posts_id = $comment['posts_id'];
        $noticeModel->title = '您的评论收到了一个喜欢';
        $noticeModel->content = $user['user_name'] . '喜欢了您的评论。';
        $noticeModel->is_read = 0;
        $noticeModel->notice_type = 5;
        $noticeModel->save();
        return $this->success([],416003);
    }

    /**
     * 删除评论
     * @param Request $request
     */
    public function delComment(Request $request)
    {
        $commentId = $request->id;
        WxComment::where('id', $commentId)
        ->update(['comment_content' => '（该评论已被删除）','comment_img_url' => '']);
//        WxComment::where('id',$commentId)->delete();
//        WxComment::where('comment_id',$commentId)->delete();
        return $this->success();
    }


}
